<?php

namespace app\backend\controller\sys;

use app\common\controller\Basic;
use app\common\service\sys\ClientService;

/**
 * 客户端
 * @author kaige
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 * @update 2022-06-27
 */
class Client extends Basic
{

  // 初始化
  protected function initialize()
  {
    parent::initialize();
    // 当前控制器服务实例化
    $this->service = new ClientService();
  }

  /**
   * 列表
   * @return array|string
   */
  public function index()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      return json($this->service->_list($data));
    }
    return _jerr(1000);
  }

  /**
   * 添加
   * @return array|string
   */
  public function add()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      return json($this->service->saveForm($data));
    }
    if ($this->request->isGet()) {
      $data = [];
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 编辑
   * @return array|string
   */
  public function edit()
  {
    $id = input('id/d', 0);
    if ($id === 0) {
      return _jerr(1022);
    }
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      return json($this->service->saveForm($data));
    }
    if ($this->request->isGet()) {
      $data = $this->service->getInfoById($id);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 添加编辑-表单数据处理
   * @param array $data
   */
  protected function _form_filter(&$data)
  {
    url_decode($data); // 前端已 encodeURIComponent 编码（最深二维数组） 后端需要解码
    $data['status'] = checkon('status', $data);
    //面试
    if (isset($data['last_time']) && !empty($data['last_time'])) {
      $data['last_time'] = str2time('last_time', $data);
    }
  }

  /**
   * 删除
   */
  public function del()
  {
    if ($this->request->isPost()) {
      $id = input('id', '');
      if ($id === 0) {
        return _jerr(1002);
      }
      return json($this->service->_del($id));
    }
    return _jerr(1000);
  }
}
